package cn.zyl.demo.service;

import cn.zyl.demo.api.DemoApiService;
import cn.zyl.demo.api.model.DemoApiModel;
import cn.zyl.demo.dao.entity.DemoDO;
import cn.zyl.demo.dao.mapper.DemoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * 类说明: api接口实现类
 *
 * @author wsz
 * @version v1.0
 * @date 2020-07-15
 */
@Service
public class DemoServiceImpl implements DemoApiService {
    @Autowired
    private DemoMapper demoMapper;
    /**Cglib的BeanCopier进行Bean拷贝的理由是,其性能要比Spring的BeanUtils,Apache的BeanUtils和PropertyUtils要好很多,尤其是数据量比较大的情况下*/
    private static final BeanCopier COPIER = BeanCopier.create(DemoApiModel.class, DemoDO.class,false);

    @Override
    public String getApiName(Long id) {
        DemoDO userDO = demoMapper.getById(id);
        return userDO != null ? userDO.getName() : null;
    }

    @Override
    public DemoApiModel queryDemoApiModel(DemoApiModel user) {
        DemoDO userDO = new DemoDO();
        COPIER.copy(user, userDO, null);
        Long id = demoMapper.insert(userDO);
        user.setId(id);
        return user;
    }
}
